/*
 * @Author: AuroCaffe 18509564594@163.com
 * @Date: 2025-01-06 18:51:49
 * @LastEditors: AuroCaffe 18509564594@163.com
 * @LastEditTime: 2025-01-06 18:51:49
 * @FilePath: \HuiLin\utils\timerCleaner.js
 * @Description: 定时器清理工具 - 防止内存泄漏
 */

class TimerCleaner {
    constructor() {
        this.timers = new Map();
        this.pageTimers = new Map();
    }

    /**
     * 注册定时器
     * @param {string} pageId 页面ID
     * @param {string} timerId 定时器ID
     * @param {number} timer 定时器对象
     */
    registerTimer(pageId, timerId, timer) {
        if (!this.pageTimers.has(pageId)) {
            this.pageTimers.set(pageId, new Map());
        }
        
        this.pageTimers.get(pageId).set(timerId, timer);
        this.timers.set(timerId, { pageId, timer });
        
        console.log(`[TimerCleaner] 注册定时器: ${pageId} - ${timerId}`);
    }

    /**
     * 清理页面定时器
     * @param {string} pageId 页面ID
     */
    clearPageTimers(pageId) {
        const pageTimerMap = this.pageTimers.get(pageId);
        if (pageTimerMap) {
            pageTimerMap.forEach((timer, timerId) => {
                if (timer) {
                    clearInterval(timer);
                    clearTimeout(timer);
                }
                this.timers.delete(timerId);
            });
            this.pageTimers.delete(pageId);
            console.log(`[TimerCleaner] 清理页面定时器: ${pageId}`);
        }
    }

    /**
     * 清理特定定时器
     * @param {string} timerId 定时器ID
     */
    clearTimer(timerId) {
        const timerInfo = this.timers.get(timerId);
        if (timerInfo) {
            const { pageId, timer } = timerInfo;
            
            if (timer) {
                clearInterval(timer);
                clearTimeout(timer);
            }
            
            this.timers.delete(timerId);
            
            const pageTimerMap = this.pageTimers.get(pageId);
            if (pageTimerMap) {
                pageTimerMap.delete(timerId);
                if (pageTimerMap.size === 0) {
                    this.pageTimers.delete(pageId);
                }
            }
            
            console.log(`[TimerCleaner] 清理定时器: ${timerId}`);
        }
    }

    /**
     * 清理所有定时器
     */
    clearAllTimers() {
        this.timers.forEach((timerInfo, timerId) => {
            const { timer } = timerInfo;
            if (timer) {
                clearInterval(timer);
                clearTimeout(timer);
            }
        });
        
        this.timers.clear();
        this.pageTimers.clear();
        
        console.log('[TimerCleaner] 清理所有定时器');
    }

    /**
     * 获取统计信息
     */
    getStats() {
        const stats = {
            totalTimers: this.timers.size,
            totalPages: this.pageTimers.size,
            pageStats: {}
        };

        this.pageTimers.forEach((timerMap, pageId) => {
            stats.pageStats[pageId] = timerMap.size;
        });

        return stats;
    }

    /**
     * 检查是否有定时器泄漏
     */
    checkLeaks() {
        const stats = this.getStats();
        if (stats.totalTimers > 20) {
            console.warn(`[TimerCleaner] 检测到可能的定时器泄漏: ${stats.totalTimers} 个定时器`);
            return true;
        }
        return false;
    }
}

// 创建单例实例
const timerCleaner = new TimerCleaner();

export default timerCleaner;
